// Job
// 
// 保佑没Bug 
//
// Created by AS on 2019/5/4. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "XLUserModel.h"

@implementation XLUserModel

MJCodingImplementation

+(void)saveUserInfo:(XLUserModel *)user{
    [NSKeyedArchiver archiveRootObject:user toFile:UserPath];
}

+(void)removeUserInfo{
    [[NSFileManager defaultManager] removeItemAtPath:UserPath error:nil];
}

+(XLUserModel *)getUserInfo{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:UserPath];;
}


+(void)getServeUserInfo{
    
    XLAuthModel *auth = [XLAuthModel getAuth];
    if (!auth) {
        return;
    }
    
    LocationManager *location = [LocationManager manager];
    UIDevice *device = [UIDevice currentDevice];
    NSString *url = [NSString stringWithFormat:@"/api/user/%@",auth.userID];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"city_id"] = @"0";
    dict[@"device_id"] = device.identifierForVendor.UUIDString;
    dict[@"latitude"] = @(location.coordinate2D.latitude);
    dict[@"longitude"] = @(location.coordinate2D.longitude);
    [HTTPTools GET:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                XLUserModel *user = [XLUserModel mj_objectWithKeyValues:result];
                [XLUserModel saveUserInfo:user];
            }
        }
    }];
    
}


@end


@implementation XLAuthModel

MJCodingImplementation

/** 保存用户信息到本地 */
+(void)saveAuth:(XLAuthModel *)auth{
    [NSKeyedArchiver archiveRootObject:auth toFile:[PathDocument stringByAppendingPathComponent:@"auth.archiver"]];
}

/** 获取本地用户信息 */
+(XLAuthModel *)getAuth{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[PathDocument stringByAppendingPathComponent:@"auth.archiver"]];
}

/** 移除用户信息 */
+(void)removeAuth{
    [[NSFileManager defaultManager] removeItemAtPath:[PathDocument stringByAppendingPathComponent:@"auth.archiver"] error:nil];
}

@end
